import java.io.*;
import java.net.*;
import java.util.*;

public class UDPPresenceServerThread extends Thread {
	
	private ServerSocket _server;
	private ArrayList<InetSocketAddress> _SocketAddressList;
	
	
	
	public UDPPresenceServerThread() throws IOException {
		_SocketAddressList = new ArrayList<InetSocketAddress>();
		try {
			_server = new ServerSocket(8000);
			System.out.println("SHIT");
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void run() {
		System.out.println("==== Presence Server ====");
		while(true){
			try {
				System.out.println("Waiting for a client connection...");
				Socket client = _server.accept();
				System.out.println("Accepted a connection from: " + client.getInetAddress() + ":" + client.getPort());
				_SocketAddressList.add(new InetSocketAddress(client.getInetAddress(), client.getPort()));
				//TO DO : send broadcast around here (blabla has went online etc.)
                ObjectOutputStream outStream = new ObjectOutputStream(client.getOutputStream());
                outStream.writeObject(_SocketAddressList);
                outStream.flush();
                outStream.close();
				
			} 
			catch (IOException e) {
				
				e.printStackTrace();
			} 
		}
	}


}
